<?php 
    class CartComponent extends Component{
        public $components=array('Session');
        public function checkItem($id=null){
            $check=true;
            if($this->Session->read('cart')){
                $cart=$this->Session->read('cart');
                foreach($cart as $data){
                    if($id==$data['id']){
                        $check=false;
                        break;
                    }
                }
            }
            return $check;
        }
        public function createItem($node=null){
            if(is_array($node)){
                $new['id']=$node['id'];
                $new['name']=$node['title'];
                $new['price']=$node['field_price'];
                $new['number']=1;
                if($node['field_old_price']){
                    $new['promotion']=ceil(($node['field_old_price']-$node['field_price'])/$node['field_old_price']*100);
                }
                return $new;
            }
            return false;
        }
        public function changeItem($id=null,$value=null){
            $check=true;
            if($this->Session->read('cart')){
                $cart=$this->Session->read('cart');
                foreach($cart as $key=> $data){
                    if($id==$data['id']){
                        $this->Session->write('cart.'.$key.'.number',$value);
                        break;
                    }
                }
            }
            return $check;
        }
        public function removeItem($id=null){
            $check=true;
            if($this->Session->read('cart')){
                $cart=$this->Session->read('cart');
                foreach($cart as $key=> $data){
                    if($id==$data['id']){
                        $this->Session->delete('cart.'.$key);
                        break;
                    }
                }
            }
            return $check;
        }
        public function updateSumCart(){
            if($this->Session->read('cart')){
                $cart=$this->Session->read('cart');
                $sum=0;
                foreach($cart as $key=> $data){
                    if($data){
                        $sum+=$data['number']*$data['price'];
                    }
                }
                return $sum;
            }
        }
        public function changeTime($time=null){
            $this->Session->write('timeOrder',$time);
        }
        public function formatTime($time=null){
            $day=substr($time,0,2);
            $month=substr($time,3,2);
            $year=substr($time,6);
            return $month.'/'.$day.'/'.$year;
        }
        public function formatCurrency($value=null){
            $result='';
            while(strlen($value)>3){
                $sub=substr($value,-3);
                $value=substr($value,0,strlen($value)-3);
                $result='.'.$sub.$result;
            }
            return $value.$result;
        }
    }
?>